package net.minecraft.util.packrat;

import java.util.Optional;
import org.jetbrains.annotations.Nullable;

public interface ParsingState<S> {
	ParseResults getResults();

	ParseErrorList<S> getErrors();

	default <T> Optional<T> startParsing(ParsingRuleEntry<S, T> rule) {
		T object = this.parse(rule);
		if (object != null) {
			this.getErrors().setCursor(this.getCursor());
		}

		if (!this.getResults().areFramesPlacedCorrectly()) {
			throw new IllegalStateException("Malformed scope: " + this.getResults());
		} else {
			return Optional.ofNullable(object);
		}
	}

	@Nullable
	<T> T parse(ParsingRuleEntry<S, T> rule);

	S getReader();

	int getCursor();

	void setCursor(int cursor);

	Cut pushCutter();

	void popCutter();

	ParsingState<S> getErrorSuppressingState();
}
